package me.work.pay.jsyl.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import java.util.HashMap;
import java.util.Map;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.work.pay.jsyl.app.utils.RxUtil;
import me.work.pay.jsyl.mvp.contract.OrderListContract;
import me.work.pay.jsyl.mvp.model.entity.OrderData;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/14/2020 08:45
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class OrderListPresenter extends BasePresenter<OrderListContract.Model, OrderListContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public OrderListPresenter(OrderListContract.Model model, OrderListContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void get_orders() {
        Map<String, Object> map=new HashMap<>();
        map.put("pageIndex",0);
        mModel.search_order(map)
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<OrderData>(mErrorHandler) {
                    @Override
                    public void onNext(OrderData data) {
                        if(("success").equals(data.result_code)){
                            mRootView.load_success(data.statistics,data.showList);
                        }else {
                            mRootView.load_error();
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        mRootView.load_error();
                    }
                });
    }
}
